package com.boco.fznq.main;


import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.boco.fznq.R;
import com.boco.fznq.classify.ProjectFragment;
import com.boco.fznq.home.HomeFragment;
import com.boco.fznq.personal.PersonalFragment;
import com.boco.fznq.view.TopbarView;
import com.culiu.core.activity.BaseCoreMVPActivity;
import com.culiu.core.utils.debug.DebugLog;
import com.culiu.core.utils.notification.ToastUtils;

public class MainActivity extends BaseCoreMVPActivity<MainPresenter, IMainUI> implements IMainUI {


    private TopbarView topbarView;

    private RadioGroup mRadioGroutp;


    private HomeFragment homeFragment;
    private PersonalFragment personalFragment;
    private ProjectFragment classifyFragment;
    private RadioButton mRbHome;
    private RadioButton mRbProject;
    private RadioButton mRbPersonal;

    FragmentManager fragmentManager;


    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected IMainUI getUi() {
        return this;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        topbarView = (TopbarView) findViewById(R.id.main_topbar);
        topbarView.setMiddleText("主页");
        topbarView.setLeftImgGone(true);

        mRadioGroutp = (RadioGroup) findViewById(R.id.radio_group_btn);
        mRbHome = (RadioButton) findViewById(R.id.rb_home);
        mRbProject = (RadioButton) findViewById(R.id.rb_classify);
        mRbPersonal = (RadioButton) findViewById(R.id.rb_person);

        fragmentManager = getSupportFragmentManager();
        showHomeFragment();
        mRadioGroutp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        showHomeFragment();
                        break;
                    case R.id.rb_classify:
                        showClassifyFragment();
                        break;
                    case R.id.rb_person:
                        showPersonalFragment();
                        break;
                }
            }
        });
    }

    public void showHomeFragment() {
        topbarView.setMiddleText("主页");
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);
        mRbHome.setChecked(true);
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            transaction.add(R.id.main_frame, homeFragment);
        } else {
            transaction.show(homeFragment);
        }
        transaction.commit();
    }

    public void showClassifyFragment() {
        topbarView.setMiddleText("农地经营权交易信息");
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);
        mRbProject.setChecked(true);
        if (classifyFragment == null) {
            classifyFragment = new ProjectFragment();
            transaction.add(R.id.main_frame, classifyFragment);
        } else {
            transaction.show(classifyFragment);
        }
        transaction.commit();
    }

    public void showPersonalFragment() {
        topbarView.setMiddleText("我的");
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);
        mRbPersonal.setChecked(true);
        if (personalFragment == null) {
            personalFragment = new PersonalFragment();
            transaction.add(R.id.main_frame, personalFragment);
        } else {
            transaction.show(personalFragment);
        }
        transaction.commit();
    }


    private void hideAllFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (classifyFragment != null) {
            transaction.hide(classifyFragment);
        }

        if (personalFragment != null) {
            transaction.hide(personalFragment);
        }
        mRbHome.setChecked(false);
        mRbProject.setChecked(false);
        mRbPersonal.setChecked(false);
    }


    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    private long exitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    private String mPayParams;

    @Override
    protected void onResume() {
        super.onResume();
        mPayParams = getIntent().getStringExtra("from_bankabc_param");

        if (mPayParams != null) {
            DebugLog.d("jiangcheng", "支付返回的结果为:  " + mPayParams);

            ToastUtils.showLong(this, mPayParams);
        }
    }
}
